package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.PushHistoryDto;
import org.hibernate.query.NativeQuery;
import org.hibernate.transform.Transformers;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.xml.bind.SchemaOutputResolver;
import java.util.ArrayList;
import java.util.List;

public class PushHistoryRepositoryImpl {

    @PersistenceContext
    private EntityManager em;

    public List<PushHistoryDto> getPushHistoryByOrgIdAndActorId(Integer orgId,
                                                                String actorIds,
                                                                Integer mCount,
                                                                Integer mSize) throws Exception {
        String sql = "select * from tb_push_history " +
                "where orgId = :orgId and actorId in( + " + actorIds + ")" +
                "order by pushTime desc";
        Query query = em.createNativeQuery(sql);
        query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(PushHistoryDto.class));
        query.setParameter("orgId", orgId);
        return query.setFirstResult((mCount - 1) * mSize).setMaxResults(mSize).getResultList();
    }



    public List<PushHistoryDto> getFaultPushList(Integer orgId,
                                                          Integer pushType,
                                                          Integer mCount,
                                                          Integer mSize)throws Exception{
        String sql="select PushHistoryId,OrgId,pushType,PushContent,ActorId,PushTime,DeviceId from tb_push_history where " +
                "OrgId="+orgId+" and PushType="+pushType+" order by pushTime desc";
        Query query = em.createNativeQuery(sql);
        query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(PushHistoryDto.class));

        return query.setFirstResult((mCount - 1) * mSize).setMaxResults(mSize).getResultList();

    }

}
